package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.constant.MessageConstant;
import com.itcast.dao.MemberDao;
import com.itcast.dto.InsertMemberDTO;
import com.itcast.dto.MemberLoginDTO;
import com.itcast.entity.Result;
import com.itcast.pojo.Member;
import com.itcast.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @className: MemberServiceImpl
 * @description: TODO 类描述
 * @author: Jng
 * @date: 2022/4/18
 **/
@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;

    /**
     * 根据月份查询会员数量
     *
     * @param months
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";
            Integer memberCountByDate = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(memberCountByDate);
        }
        return memberCount;
    }

    @Override
    public Result login4EmailAndPwd(MemberLoginDTO memberLoginDTO) {
        Member member=  memberDao.login4EmailAndPwd(memberLoginDTO.getPhoneNumber(), memberLoginDTO.getPassword());
        if (member==null){
            return new Result(false,"401","账号或密码错误");
        }
        return new Result(true,"200",member);
    }

    @Override
    public Result registerMember(Member member) {
        if (Objects.isNull(member.getPhoneNumber())){
            return new Result(false,"用户手机号码不能为空");
        }
        if (Objects.isNull(member.getPassword())){
            return new Result(false,"用户密码不能为空");
        }
        boolean flag=memberDao.registerMember(member);
        if (flag){
            return new Result(flag, MessageConstant.REGISTER_MEMBER_SUCESS);
        }
        return new Result(flag,MessageConstant.REGISTER_MEMBER_FAIL);
    }

    @Override
    public Result getUserDetails(Integer id) {
       Member member= memberDao.getUserDetails(id);
       if (Objects.isNull(member)){
           return new Result("401","获取用户详情信息失败");
       }
        return new Result(true,"200",member);
    }

    @Override
    public Result updateUserDetails(Member member) {
        boolean flag=memberDao.updateUserDetails(member);
            return new Result(true,"用户信息修改成功");
    }

    @Override
    public Result addUser(InsertMemberDTO insertMemberDTO) {
        boolean flag=memberDao.addUser(insertMemberDTO);
        if (flag){
            return new Result(flag,MessageConstant.ADD_MEMBER_SUCCESS);
        }
        return new Result(flag,MessageConstant.ADD_MEMBER_FAIL);
    }
}
